package com.test.memory.leak;

import org.apache.log4j.Logger;

public class ReportManager extends Thread {
	static final Logger log = Logger.getLogger(ReportManager.class);

	private static BoSession boSession;

	private ReportGen[] reportGenTasks = new ReportGen[30];
	
	

	private boolean initBoSession() {
		boSession = new BoSession();
		return true;
	}

	public ReportManager() {
		super("ReportManager");
		boolean initbo = initBoSession();
		if (!initbo)
			return;
		for (int i = 0; i < reportGenTasks.length; i++) {
			reportGenTasks[i] = new ReportGen(String.valueOf(i));
			reportGenTasks[i].setBosess(boSession);
			reportGenTasks[i].start();
		}
		log.info("30 ReportGen task(s) thread started. ");
	}

	public void run() {
		while (true) {
			distributeReportTasks();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean distributeReportTasks() {

		if (!isAllReportGenTaskDone()) {
			log.info("Not all ReportGen tasks finished.");
			return true;
		}

		for (int i = 0; i < 30; i++) {
			reportGenTasks[i].setJob(i);
			synchronized (reportGenTasks[i]) {
				reportGenTasks[i].notify();
			}
		}
		log.info("30 ReportGen tasks distributed.");

		return true;
	}

	private boolean isAllReportGenTaskDone() {
		for (ReportGen reportGen : reportGenTasks) {
			if (!reportGen.isDone())
				return false;
		}
		return true;
	}

}
